﻿namespace YidanSoft.Library.EditorUtility.WinControls
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;
    using YidanSoft.Library.EditorUtility.General;
    using YidanSoft.Library.EditorUtility.Win32;

    [ToolboxItem(false)]
    public class TextBoxEx : TextBox
    {
        private DrawState drawState = DrawState.Normal;

        public TextBoxEx()
        {
            this.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        }

        private void DrawTextBoxState(DrawState drawState)
        {
            YidanSoft.Library.EditorUtility.Win32.RECT rect = new YidanSoft.Library.EditorUtility.Win32.RECT();
            WindowsAPI.GetWindowRect(base.Handle, ref rect);
            Rectangle rectangle = new Rectangle(0, 0, rect.right - rect.left, rect.bottom - rect.top);
            IntPtr windowDC = WindowsAPI.GetWindowDC(base.Handle);
            using (Graphics graphics = Graphics.FromHdc(windowDC))
            {
                Pen pen;
                using (pen = new Pen(SystemBrushes.Window))
                {
                    graphics.DrawRectangle(pen, (int) (rectangle.Left + 1), (int) (rectangle.Top + 1), (int) (rectangle.Width - 3), (int) (rectangle.Height - 3));
                }
                if (drawState == DrawState.Normal)
                {
                    using (pen = new Pen(SystemBrushes.Window))
                    {
                        graphics.DrawRectangle(pen, rectangle.Left, rectangle.Top, rectangle.Width - 1, rectangle.Height - 1);
                    }
                }
                else if (drawState == DrawState.Hot)
                {
                    graphics.DrawRectangle(SystemPens.Highlight, rectangle.Left, rectangle.Top, rectangle.Width - 1, rectangle.Height - 1);
                }
                else if (drawState == DrawState.Disable)
                {
                    graphics.FillRectangle(SystemBrushes.Window, rectangle);
                    Size size = TextUtil.GetTextSize(graphics, this.Text, this.Font);
                    Point point = new Point(rectangle.Left + 1, rectangle.Top + ((rectangle.Height - size.Height) / 2));
                    if (base.PasswordChar != '\0')
                    {
                        int length = this.Text.Length;
                        string s = new string(base.PasswordChar, length);
                        graphics.DrawString(s, this.Font, SystemBrushes.ControlDark, (PointF) point);
                    }
                    else
                    {
                        graphics.DrawString(this.Text, this.Font, SystemBrushes.ControlDark, (PointF) point);
                    }
                }
            }
            WindowsAPI.ReleaseDC(base.Handle, windowDC);
        }

        protected override void OnGotFocus(EventArgs e)
        {
            base.OnGotFocus(e);
            this.drawState = DrawState.Hot;
            base.Invalidate();
        }

        protected override void OnLostFocus(EventArgs e)
        {
            base.OnLostFocus(e);
            this.drawState = DrawState.Normal;
            base.Invalidate();
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            this.drawState = DrawState.Hot;
            base.Invalidate();
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            if (!base.ContainsFocus)
            {
                this.drawState = DrawState.Normal;
                base.Invalidate();
            }
        }

        protected override void WndProc(ref Message m)
        {
            bool flag = true;
            if (m.Msg == 15)
            {
                base.WndProc(ref m);
                this.DrawTextBoxState(base.Enabled ? this.drawState : DrawState.Disable);
                flag = false;
            }
            if (flag)
            {
                base.WndProc(ref m);
            }
        }

        public System.Windows.Forms.BorderStyle BorderStyle
        {
            get
            {
                return base.BorderStyle;
            }
            set
            {
                if (value != System.Windows.Forms.BorderStyle.Fixed3D)
                {
                    string paramName = "BorderStyle can only be Fixed3D for this class";
                    ArgumentException exception = new ArgumentException("BorderStyle", paramName);
                    throw exception;
                }
                base.BorderStyle = value;
            }
        }
    }
}

